package com.sjgl.esdn.service.impl;

import com.sjgl.esdn.constant.UserConstantsESDN;
import com.sjgl.esdn.domain.entity.PwdReset;
import com.sjgl.esdn.domain.entity.User;
import com.sjgl.esdn.mapper.DeptMapper;
import com.sjgl.esdn.mapper.UserManageMapper;
import com.sjgl.esdn.mapper.UserMapper;
import com.sjgl.esdn.service.UserManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户管理 服务层
 * @author 虾米
 */
@Service
public class UserManageServiceImpl implements UserManageService{

    private static final Logger log = LoggerFactory.getLogger(UserManageServiceImpl.class);

    @Autowired
    private UserManageMapper userManageMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUserList(Long userId) {
        int type = userManageMapper.selectUserType(userId);
        List<User> list;
        if(type == UserConstantsESDN.USER_ADMIN){
            list = userManageMapper.getUserList();
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }else{
            list = userManageMapper.selectListByLeader(userManageMapper.getDeptId(userId));
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }
        return list;
    }

    @Override
    public List<User> getUserListByDeptName(String deptName){
        if(deptMapper.checkDeptExistUser(deptMapper.getIdByDeptName(deptName))>0){
            List<User> list = userManageMapper.selectListByLeader(userManageMapper.selectLeaderByDeptName(deptName));
            return list;
        }else{
            return null;
        }
    }


    @Override
    public List<User> selectByKeyword(String keyword,Long userId) {
        int type = userManageMapper.selectUserType(userId);
        List<User> list;
        if(type == UserConstantsESDN.USER_ADMIN){
            list = userManageMapper.selectByKeyword(keyword);
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }else{
            list = userManageMapper.selectByKeywordByLeader(keyword,userManageMapper.getDeptId(userId));
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }
        return list;
    }

    @Override
    public List<User> showApproval(Long userId) {
        int type = userManageMapper.selectUserType(userId);
        List<User> list;
        if(type == UserConstantsESDN.USER_ADMIN){
            list = userManageMapper.selectByUserType(0);
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }else{
            list = userManageMapper.selectByUserTypeByLeader(userManageMapper.getDeptId(userId));
            for (User user: list) {
                user.setLeaderName(userManageMapper.selectLeaderName(user.getDeptId()));
            }
        }
        return list;
    }

    @Override
    public int approveRegister(Long userid,Long oprtId,Date time) {
        return userManageMapper.updateRegister(userid,oprtId,time);
    }

    @Override
    public List<PwdReset> showResetList(Long userId) {
        int type = userManageMapper.selectUserType(userId);
        List<PwdReset> list;
        if(type == UserConstantsESDN.USER_ADMIN){
            list = userManageMapper.selectResetPwdUser();
            for (PwdReset pwd: list) {
                pwd.setLeaderName(userManageMapper.selectLeaderName(userManageMapper.selectDeptId(pwd.getUserId())));
            }
        }else{
            list = userManageMapper.selectResetPwdByLeader(userManageMapper.getDeptId(userId));
            for (PwdReset pwd: list) {
                pwd.setLeaderName(userManageMapper.selectLeaderName(userManageMapper.selectDeptId(pwd.getUserId())));
            }
        }
        return list;
    }

    @Override
    public int resetPassword(Long userId,Long oprtId, Date time) {
        return userManageMapper.updatePassword(userId,oprtId,time);
    }

    @Override
    public int updateStatus(Long userId, Long oprtId, Date time) {
        return userManageMapper.updateStatus(userId,oprtId,time);
    }

    @Override
    public int rejectStatus(Long userId, Long oprtId, Date time){
        return userManageMapper.rejectStatus(userId,oprtId,time);
    }

    @Override
    public int modifyUserType(Long userId, String deptName, int userType,Long oprtId,Date time) {
        //若涉及到管理员级别权限的修改，还要调整dept表的leader
        User user=userMapper.selectUserById(userId);
        //原部门leader重置为0
        if(user.getUserType()==2){
            deptMapper.setDeptLeader(deptMapper.getIdByDeptName(user.getDeptName()),0L);
        }
        //现部门leader重置为userId
        if(userType==2){
            deptMapper.setDeptLeader(deptMapper.getIdByDeptName(deptName),user.getUserId());
        }

        //修改用户表相关信息
        return userManageMapper.updateUserType(userId,deptName,userType,oprtId,time);
    }

    @Override
    public int discardUser(Long userId,Long oprtId,Date time) {
        return userManageMapper.updateDiscard(userId,oprtId,time);
    }

}
